GOOS=$(shell go env GOOS)
GOPATH=$(shell go env GOPATH)

VERSION=$(shell git describe --always --match "v[0-9]*" HEAD)

.DEFAULT_GOAL := all

.PHONY: version
version:
	@echo ${VERSION}

.PHONY: all
all: install-protobuf generate-protobuf-go-file gotidy

.PHONY: install-protobuf
install-protobuf:
	go get -u -d google.golang.org/protobuf
	go get -u -d github.com/golang/protobuf/protoc-gen-go
	sudo echo "export GOPATH=${GOPATH}" >> /etc/profile
	sudo echo "export PATH=$$PATH:${GOPATH}/bin" >> /etc/profile
	$(shell source /etc/profile)

	@echo $(GOOS)
	@if [ $(GOOS) = "linux" ]; then \
	  	wget https://github.com/protocolbuffers/protobuf/releases/download/v3.17.3/protoc-3.17.3-linux-x86_64.zip; \
        unzip protoc-3.17.3-linux-x86_64.zip -d /usr/local/protobuf/; \
	elif [ $(GOOS) = "darwin" ]; then \
		 wget https://github.com/protocolbuffers/protobuf/releases/download/v3.19.0/protoc-3.19.0-osx-x86_64.zip; \
         unzip protoc-3.19.0-osx-x86_64.zip -d /usr/local/protobuf/; \
    elif [ $(GOOS) = "window" ]; then \
		wget https://github.com/protocolbuffers/protobuf/releases/download/v3.19.0/protoc-3.19.0-win64.zip; \
		unzip protoc-3.19.0-win64.zip -d .\; \
	else \
		echo "os unsupport"; \
	fi

.PHONY: generate-protobuf-go-file
generate-protobuf-go-file:
ifneq ($(wildcard ./proto/*.pb.go),)
	rm ./proto/*.pb.go
endif
	@echo $(GOOS)
	@if [ $(GOOS) = "window" ]; then \
		cd ./bk-proto/;../protoc --proto_path=./ --go_out=plugins=grpc:. ./*;cd ../; \
	else \
		cd ./bk-proto/;/usr/local/protobuf/bin/protoc --proto_path=./ --go_out=plugins=grpc:. ./*;cd ../; \
	fi


.PHONY: gotidy
gotidy:
	rm -fr go.sum
	go mod tidy -go=1.16
	go mod tidy -go=1.17